/**
 * @param {number} pageDataNum 一共多少条数据
 * @param {number} num 设置每页多少条数据
 * @param {boolean} flag  设置每页是否带表头，true带表头，false不带
 */
function setPage(pageDataNum, num, flag) {
    let pageNum;  //一共多少页
    let trNum = 0; //用于遍历的数
    let originHtml = document.getElementsByClassName("layui-container-fluid")[0];//要操作的原始页面
    let flag=true;
  /*   while(flag){
        let i=0;
        if(originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[i].style.pageBreakBefore=="always"){
            console.log("换页")
            let divLayui = document.createElement("div");
            divLayui.setAttribute("class", "layui-container-fluid")
         }
         if()
         flag=false
    } */
   for(let i=0;i<pageDataNum;i++){
        console.log(originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[i].style.pageBreakBefore=="always")
        if(originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[i].style.pageBreakBefore=="always"){
           console.log("换页")
           let divLayui = document.createElement("div");
           divLayui.setAttribute("class", "layui-container-fluid")
           //设置整个页面的高度，使打印在一个页面上
           /* divLayui.style.height = "1450px" */
           //创建boxdiv(是divLayui的子元素)
           let box = document.createElement("div");
           box.setAttribute("class", "box")
           let divRpt = document.createElement("div");
           divRpt.setAttribute("id", "rpt_header");
           divRpt.innerHTML = originHtml.querySelector("#rpt_header").innerHTML;
           //设置页数，第几页
           divRpt.querySelector("#rpt_npage").innerHTML = pageNum
           divRpt.querySelector("#rpt_tpage").innerHTML = i + 1;
           box.appendChild(divRpt)
           let style = document.createElement("style")
           style.innerHTML = document.getElementsByClassName("box")[0].getElementsByTagName("style")[0].innerHTML;
           box.appendChild(style)
           divLayui.appendChild(box)
           //创建table（是box的子元素)
           let table = document.createElement("table");
           table.setAttribute("class", "tg");
           table.setAttribute("style", "undefined;table-layout: fixed; width: 1800px")
           //创建colgroup（是table的子元素)
           let colgroup = document.createElement("colgroup");
           colgroup.innerHTML = document.getElementsByTagName("colgroup")[0].innerHTML
           table.appendChild(colgroup)
           if (flag == true) {
               console.log("是否带有表头")
               let thead = document.createElement("thead");
               thead.innerHTML = originHtml.querySelector("thead").innerHTML
               table.appendChild(thead)
           }
        
           //创建tbody（是table的子元素)
           let tbody = document.createElement("tbody");
           for (let j = 0; j < num; j++) {
               let tr = document.createElement("tr")
               if(trNum<pageDataNum){
                   tr.innerHTML = originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[trNum].innerHTML;
               }
               trNum++;
               tbody.appendChild(tr);
           }
           table.appendChild(tbody)
           box.appendChild(table)
           //创建divFooter(是table的子元素)
           let divFooter = document.createElement("div")
           divFooter.setAttribute("id", "rpt_footer")
           divFooter.innerHTML = originHtml.querySelector("#rpt_footer").innerHTML
           box.appendChild(divFooter)
           document.getElementsByTagName("body")[0].insertBefore(divLayui, originHtml)
        }
    } 



    pageNum = Math.ceil(pageDataNum / num);  //得到多少页
for (let i = 0; i < pageNum; i++) {

        //创建最外层div
        let divLayui = document.createElement("div");
        divLayui.setAttribute("class", "layui-container-fluid")
        //设置整个页面的高度，使打印在一个页面上
        /* divLayui.style.height = "1450px" */
        //创建boxdiv(是divLayui的子元素)
        let box = document.createElement("div");
        box.setAttribute("class", "box")
        let divRpt = document.createElement("div");
        divRpt.setAttribute("id", "rpt_header");
        divRpt.innerHTML = originHtml.querySelector("#rpt_header").innerHTML;
        //设置页数，第几页
        divRpt.querySelector("#rpt_npage").innerHTML = pageNum
        divRpt.querySelector("#rpt_tpage").innerHTML = i + 1;
        box.appendChild(divRpt)
        let style = document.createElement("style")
        style.innerHTML = document.getElementsByClassName("box")[0].getElementsByTagName("style")[0].innerHTML;
        box.appendChild(style)
        divLayui.appendChild(box)
        //创建table（是box的子元素)
        let table = document.createElement("table");
        table.setAttribute("class", "tg");
        table.setAttribute("style", "undefined;table-layout: fixed; width: 1800px")
        //创建colgroup（是table的子元素)
        let colgroup = document.createElement("colgroup");
        colgroup.innerHTML = document.getElementsByTagName("colgroup")[0].innerHTML
        table.appendChild(colgroup)
        if (flag == true) {
            console.log("是否带有表头")
            let thead = document.createElement("thead");
            thead.innerHTML = originHtml.querySelector("thead").innerHTML
            table.appendChild(thead)
        }
     
        //创建tbody（是table的子元素)
        let tbody = document.createElement("tbody");
        for (let j = 0; j < num; j++) {
            let tr = document.createElement("tr")
            if(trNum<pageDataNum){
                tr.innerHTML = originHtml.getElementsByTagName("tbody")[0].getElementsByTagName("tr")[trNum].innerHTML;
            }
            trNum++;
            tbody.appendChild(tr);
        }
        table.appendChild(tbody)
        box.appendChild(table)
        //创建divFooter(是table的子元素)
        let divFooter = document.createElement("div")
        divFooter.setAttribute("id", "rpt_footer")
        divFooter.innerHTML = originHtml.querySelector("#rpt_footer").innerHTML
        box.appendChild(divFooter)
        document.getElementsByTagName("body")[0].insertBefore(divLayui, originHtml)
    }
    originHtml.remove();
}
//调用方法
setPage(50, 20, true)
